package com.example.zsf.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.zsf.R;
import com.example.zsf.model.MobileModel;
import com.example.zsf.view.TicketProgress;
import com.orhanobut.logger.Logger;

public class MobileGridAdapter extends BaseQuickAdapter<MobileModel.MobileListModel, BaseViewHolder> {
    private Context context;
    private int type;
    private int[] bgs = {
            R.drawable.bg_mark_red,
            R.drawable.bg_title_orange,
            R.drawable.bg_title_yellow,
            R.drawable.bg_mark_green,
            R.drawable.bg_title_aquamarine,
            R.drawable.bg_title_blue,
            R.drawable.bg_title_purple,
    };

    public MobileGridAdapter(Context context, int layoutResId, int type) {
        super(layoutResId);
        this.context = context;
        this.type = type;
    }


    @Override
    protected void convert(BaseViewHolder baseViewHolder, MobileModel.MobileListModel mobileListModel) {
        Glide.with(context).load(mobileListModel.getAds_img()).placeholder(R.mipmap.loading).into((ImageView) baseViewHolder.getView(R.id.imageView));

        baseViewHolder.setText(R.id.title_tv, mobileListModel.getAds_name());
        baseViewHolder.setText(R.id.time_tv, mobileListModel.getAds_time());


        int randomColor1 = getRandomColor();
        int randomColor2 = getRandomColor(randomColor1);
        int randomColor3 = getRandomColor(randomColor1, randomColor2);
        baseViewHolder.setText(R.id.mark1_tv, mobileListModel.getAds_ip());
        baseViewHolder.setBackgroundResource(R.id.mark1_tv, randomColor1);
        baseViewHolder.setText(R.id.mark2_tv, mobileListModel.getAds_xl());
        baseViewHolder.setBackgroundResource(R.id.mark2_tv, randomColor2);
        baseViewHolder.setText(R.id.mark_tv3, mobileListModel.getAds_qq());
        baseViewHolder.setBackgroundResource(R.id.mark_tv3, randomColor3);

        baseViewHolder.setText(R.id.textView, type == 1 ? "电脑端" : "手机端");
        baseViewHolder.getView(R.id.textView2).setVisibility(mobileListModel.getGift_total_num() == null || mobileListModel.getGift_total_num().equals("0") ? View.GONE : View.VISIBLE);
        baseViewHolder.getView(R.id.playImageView).setVisibility(mobileListModel.getAds_eval_type() == 1 ? View.GONE : View.VISIBLE);


        //如果还没开始抢 就需要显示还有多少时间
        long cur = TimeUtils.getNowMills() / 1000;
        long target = Long.parseLong(mobileListModel.getGift_start_time());
        String fitTimeSpan = null;
        if (target > cur) {
            fitTimeSpan = TimeUtils.getFitTimeSpanByNow(target * 1000, 2);
        }
        if (mobileListModel.getGift_total_num() == null || mobileListModel.getGift_total_num().equals("0")) {
            ((TicketProgress) baseViewHolder.getView(R.id.ticket_progress)).setData(mobileListModel.getAds_ver(), null, null);
        } else {
            double percent = Double.parseDouble(mobileListModel.getGift_last_num()) / Double.parseDouble(mobileListModel.getGift_total_num());
            ((TicketProgress) baseViewHolder.getView(R.id.ticket_progress)).setData(null, percent, fitTimeSpan);
        }
    }


    private int getRandomColor(int... exceptColors) {
        if (exceptColors != null && exceptColors.length >= bgs.length) throw new AssertionError();

        while (true) {
            int v = (int) (Math.random() * 10);
            int res = bgs[v % 7];
            if (exceptColors == null) {
                return res;
            } else {
                boolean isContain = false;
                for (int e : exceptColors) {
                    if (e == res) {
                        isContain = true;
                        break;
                    }
                }
                if (!isContain) {
                    return res;
                }
            }
        }
    }
}
